package com.StarStudio.Service.Impl;

import com.StarStudio.DAO.GoodsDAO;
import com.StarStudio.Service.GoodsService;
import com.StarStudio.Service.GoodsStockService;
import com.StarStudio.Service.PanicBuyingGoodsService;
import com.ruoyi.common.core.Entity.Goods;
import com.ruoyi.common.core.Entity.GoodsChooseResult;
import com.ruoyi.common.core.Entity.GoodsStock;
import com.ruoyi.common.core.Entity.PanicBuyingGoods;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsDAO goodsDAO;

    @Autowired
    GoodsStockService goodsStockService;

    @Autowired
    PanicBuyingGoodsService panicBuyingGoodsService;

    /**
     * 条件查询
     * @param goods 商品对象
     * @return List<Goods> 商品集合
     */
    @Override
    public List<Goods> findGoodsList(Goods goods) {
        //如果是超级管理员（银行），则可以查看所有商品
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if (sysUser.isAdmin()){
            return goodsDAO.findGoodsList(goods);
        }
        //如果是普通商家，只可以查看自己家的商品
        return goodsDAO.findGoodsListByNormalStore(goods,sysUser.getUserId());
    }

    /**
     * 根据商品ID删除商品
     * @param id 商品Id
     */
    @Override
    public void DeleteGoodsById(long id) {
        //删除商品的同时，真正删除未售出的库存个体
        List<Long> unsold_goods_item_ids = goodsStockService.findUnsoldItemIdsByGoodsId(id);
        for (Long unsold_goods_item_id : unsold_goods_item_ids) {
            goodsStockService.realDelete(unsold_goods_item_id);
        }
        //逻辑删除已售出的库存个体
        List<Long> sold_goods_item_ids = goodsStockService.findSoldItemIdsByGoodsId(id);
        for (Long sold_goods_item_id : sold_goods_item_ids) {
            goodsStockService.updateDelFlag(sold_goods_item_id);
        }
        //逻辑删除对应的抢购商品
        PanicBuyingGoods byGoods_id = panicBuyingGoodsService.findByGoods_id(id);
        if (byGoods_id != null){
            byGoods_id.setDel_flag(true);
            panicBuyingGoodsService.updatePanicBuyingGoods(byGoods_id);
        }
        //删除商品时，是逻辑删除，其实还保存在数据库中
        goodsDAO.DeleteGoodsById(id);
    }

    /**
     * 新增商品
     * @param goods 商品对象
     */
    @Override
    public void addGoods(Goods goods) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        goods.setStore_id(sysUser.getUserId());
        goodsDAO.addGoods(goods);
        int stock = goods.getStock();
        //向商品库存个体表添加数据
        GoodsStock goodsStock = new GoodsStock();
        goodsStock.setGoods_id(goods.getGoods_id());
        goodsStock.setAdd_time(new Timestamp(new Date().getTime()));
        goodsStock.setHave_been_sold(false);
        goodsStock.setStore_id(sysUser.getUserId());
        goodsStock.setGoods_name(goods.getGoods_name());
        for (int i = 0; i < stock; i++) {
            goodsStockService.addGoodsStock(goodsStock);
        }
    }

    /**
     * 修改商品
     * @param goods 商品对象
     */
    public void updateGoods(Goods goods){
        int goodsNewStock = goods.getStock();
        int goodsOldStock = goodsDAO.findStockByGoodsId(goods.getGoods_id());
        if (goodsNewStock > goodsOldStock){  /*如果增加库存*/
            //向商品库存个体表添加数据
            GoodsStock goodsStock = new GoodsStock();
            goodsStock.setGoods_id(goods.getGoods_id());
            goodsStock.setAdd_time(new Timestamp(new Date().getTime()));
            goodsStock.setHave_been_sold(false);
            goodsStock.setStore_id(goods.getStore_id());
            goodsStock.setGoods_name(goods.getGoods_name());
            int addNum = goodsNewStock - goodsOldStock;
            for (int i = 0; i < addNum; i++) {
                goodsStockService.addGoodsStock(goodsStock);
            }
        }else if (goodsNewStock < goodsOldStock){ /*如果减少库存*/
            //删除多余商品库存
            int subNum = goodsOldStock - goodsNewStock;
            List<Long> goodsStockIds = goodsStockService.findGoodsStockIds(goods.getGoods_id(), goods.getStore_id());
            for (int i = 0; i < subNum; i++) {
                goodsStockService.realDelete(goodsStockIds.get(i));
            }
        }
        goodsDAO.updateGoods(goods);
    }

    /**
     * 根据商品ID查找商品
     * @param id 商品ID
     * @return 商品对象
     */
    @Override
    public Goods findByGoods_id(long id) {
        return goodsDAO.findByGoods_id(id);
    }

    /**
     * 查询总记录数
     * @return 总记录数
     */
    @Override
    public long findAllRecordNumber() {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if (sysUser.isAdmin()){
            return goodsDAO.findAllRecordNumber();
        }
        return goodsDAO.findAllRecordNumberForNormalStore(sysUser.getUserId());
    }

    @Override
    public Goods findByGoods_Item_Id(long goods_item_id) {
        return goodsDAO.findByGoodsItemId(goods_item_id);
    }

    @Override
    public List<GoodsChooseResult> getGoodsChooseResultList() {
        List<GoodsChooseResult> goodsChooseResultList = goodsDAO.getGoodsChooseResultList();
        for (GoodsChooseResult goodsChooseResult : goodsChooseResultList) {
            int Stock1 = goodsChooseResult.getStock();
            int Stock2 = 0;
            PanicBuyingGoods byGoods_id = panicBuyingGoodsService.findByGoods_id(goodsChooseResult.getGoods_id());
            if (byGoods_id != null){
                goodsChooseResult.setStock(Stock1 + Stock2);
            }else {
                goodsChooseResult.setStock(Stock1);
            }
        }
        return goodsChooseResultList;
    }

    @Override
    public int findStockByGoodsId(long goodsId) {
        return goodsDAO.findStockByGoodsId(goodsId);
    }

    @Override
    public void updateGoodsStockById(long goodsId, int stock) {
        goodsDAO.updateGoodsStockById(goodsId,stock);
    }

    @Override
    public int findGoodsPriceByGoodsId(long goods_id) {
        return goodsDAO.findGoodsPriceByGoodsId(goods_id);
    }
}
